#!/usr/bin/env python
# -*- coding: utf-8 -*-

# taskmanager.py

import random, time, Queue
from multiprocessing.managers import BaseManager

# 发送任务的队列
task_queue = Queue.Queue()
# 接收结果的队列
result_queue = Queue.Queue()

def get_task_queue():
    return task_queue

def get_result_queue():
    return result_queue

# 从BaseManager继承的QueueManager
class QueueManager(BaseManager):
    pass

def main():
    # 把两个Queue者注册到网络上，callable参数关联了Queue对象
    QueueManager.register('get_task_queue', callable=get_task_queue)
    QueueManager.register('get_result_queue', callable=get_result_queue)
    # 绑定商品5000，设置验证码'abc'
    manager = QueueManager(address=('127.0.0.1', 5000), authkey='abc')
    # 启动Queue
    manager.start()
    # 获得通过网络访问的Queue对象
    task = manager.get_task_queue()
    result = manager.get_result_queue()
    # 放几个任务进去
    for i in range(10):
        n = random.randint(0, 10000)
        print 'Put task %d...' % n
        task.put(n)
    # 从result队列读取结果
    print 'Try get result...'
    for i in range(10):
        r = result.get(timeout=10)
        print 'Result: %s' % r
    # 关闭
    manager.shutdown()

if __name__ == '__main__':
    main()

